Ext.define('CDR.widget.UserFactory', {
	extend: 'Ext.Panel',
	alias : 'widget.userfactory',
	title: CDR.I18N['user.title'],
	selectedFactoryId : 0,
	profileCenter : null,
	initComponent: function() {
		var me = this;
		me.callParent();
		me.user = Ext.createWidget('user',{pbxFactory:me});
		this.addItem(CDR.I18N['user.button.all'],'all');
		this.addItem(CDR.I18N['user.button.admin'],'admin');
		this.addItem(CDR.I18N['user.button.contact'],'contact');
	},
	layout:'anchor',
	addItem: function(name, id){
		var me = this;
		this.add({
			obId: id,
			anchor:'100%',
			xtype : 'button',
            text    : name,
			scale: 'large',
			margin: '0 0 10px 0',
			enableToggle: true,
			toggleGroup:'serviceprovider_bnt',
			handler: function(){
				me.selectedFactoryId = this.obId;
				//get list
				if (me.user) {
					var store = me.user.view.store;
					store.setProxy({
					  type: 'ajax',
					  url : 'list-user-view?type=' + me.selectedFactoryId,
					  reader: {
						  type: 'json',
						  root: 'results'
					  }
					});
					store.load();
				}
			}
		});
	},
	submitOk: function(data) {
		this.addItem(data.name, data.id);
	},
	listeners: {
		expand : function(){
			this.profileCenter.removeAll(false);
			this.profileCenter.add(this.user);
		}
	}
});

//create trunk view
Ext.define('CDR.widget.UserFactoryView', {
  extend: 'Ext.view.View',
  alias : 'widget.userfactoryview',
  targetObject: null,
  tpl: [
      '<tpl for=".">',
          '<div class="thumb-wrap">',
              '<div class="thumb">',
                  (!Ext.isIE6? '<img src="resources/app/images/{thumb}" />' : 
                  '<div style="width:76px;height:76px;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'resources/app/images/{thumb}\')"></div>'),
              '</div>',
              '<span>{name}</span>',
          '</div>',
      '</tpl>'
  ],
  itemSelector: 'div.thumb-wrap',
  multiSelect: false,
  singleSelect: true,
  cls: 'x-image-view',
  autoScroll: true,
  bodyStyle:'background-color: white;',
  initComponent: function() {
      this.store = Ext.create('Ext.data.Store', {
          fields: ['name', 'thumb', 'obId']
      });
	this.callParent();
  },
  listeners: {
	itemdblclick: function(me, r){
		if(this.targetObject){
			this.targetObject.proccessDbclick(r);
		}
	}
  }
});

Ext.define('CDR.widget.User', {
	extend: 'Ext.Panel',
	alias : 'widget.user',
	pbxFactory: null,
	layout: 'border',
	border: false,
	currentPbx: null,
	initComponent: function() {
		var me = this;
		me.callParent();
		me.view = Ext.createWidget('userfactoryview',{region:'center', targetObject: me});
		me.formContainer = Ext.create('widget.container', {
			region:'south',
			height:'50%',
			layout:'fit',
			split: true
		});
		me.add(me.formContainer);
		me.add({xtype: 'panel', region:'center', layout:'fit',items:[me.view]});
		me.userForm = Ext.createWidget('userform', {region: 'center', url:'create-user', targetObject: me});
		me.uploadfile = Ext.create('CDR.widget.FileUpload', {url: 'import-user'});
	},
	dockedItems: [{
		xtype: 'toolbar',
		items: [{
			text: CDR.I18N['user.new'],
			iconCls: 'icon-add',
			handler: function(){
				var me =  this.up('panel');
				me.formContainer.removeAll(false);
				me.userForm.getForm().reset();
				me.userForm.initData();
				me.formContainer.add(me.userForm);
			}
		}, '-',{
			text: CDR.I18N['user.import'],
			iconCls: 'icon-import',
			handler: function(){
				var me =  this.up('panel');
				me.uploadfile.show();
			}
		}, '-', '', {
			fieldLabel: CDR.I18N['label.search'],
			labelWidth: 45,
			xtype: 'textfield',
			listeners: {
				'change': function(me, newVal, oldVal){
					var me =  this.up('panel');
					me.view.store.clearFilter();
					me.view.store.filter(new Ext.util.Filter({
							filterFn: function(item) {
								var searchtest,namematch;
								var escapere = Ext.String.escapeRegex;
								searchtest = new RegExp(escapere(newVal), 'i');
								namematch = searchtest.test(item.data.name);
								return namematch;
							}
						})
					);
				}
			}
			
		}]
	}],
	submitOk: function(data, type) {
		if(type === CDR.Constant.WINDOW_NEW_MODE){
			this.view.store.add({name: data.name, obId: data.obId, thumb: data.thumb});
		} else if(type === CDR.Constant.WINDOW_EDIT_MODE){
			this.currentPbx.set('name', data.name);
		}
		
	},
	proccessDbclick: function(r){
		this.currentPbx = r;
		var id = r.data.obId;
		this.userForm.getForm().reset();
		Ext.Ajax.request({
			scope: this,
			async: false,
			url: 'user-info?userId=' + id,
			success: function(response, opts) {
				var obj = Ext.decode(response.responseText);
				//add userId to obj;
				obj.result.userId = r.data.obId;
				this.userForm.eidtData(obj);
			}
		});
		this.formContainer.removeAll(false);
		this.formContainer.add(this.userForm);
	}
});
